package chat.activities;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.xkh.xkh.MyApplication;
import com.xkh.xkh.R;
import com.google.gson.Gson;

import chat.network.http.CreateRoom;
import inter.Delegate;
import inter.ResponseStatus;
import managers.UIHandler;
import utils.TimeUtil;

/**
 * 创建房间
 * Created by Administrator on 2016/3/29.
 */
public class CreateRoomActivity extends UIHandler.BaseActivity {

    EditText et_topic;
    EditText et_name;
    EditText et_desc;
    EditText et_maxNum;

    Button btn_create;


    CreateRoom createRoom = new CreateRoom();

    Delegate createRoomResponse = new Delegate() {
        @Override
        public void onResponse(String response) {
            if (!response.startsWith("{")) return;

            Gson gson = new Gson();
            CreateRoom.ResponseCreateRoom responseCreateRoom = gson.fromJson(response,CreateRoom.ResponseCreateRoom.class);
            if (responseCreateRoom.getStatus().equals(ResponseStatus.SUCCESS))
            {
                Toast.makeText(MyApplication.share(),"创建成功",Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_createroom);
        initview();
    }

    private void initview() {
        et_topic = (EditText) findViewById(R.id.id_et_roomTopic);
        et_name = (EditText) findViewById(R.id.id_et_roomName);
        et_desc = (EditText) findViewById(R.id.id_et_roomDesc);
        et_maxNum = (EditText) findViewById(R.id.id_et_roomMaxNum);

        btn_create = (Button) findViewById(R.id.id_btn_create);
        btn_create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createRoom();
            }
        });
    }

    private void createRoom() {
        String str_topic = et_topic.getText().toString();
        String str_name = et_name.getText().toString();
        String str_desc = et_desc.getText().toString();
        String str_maxNum = et_maxNum.getText().toString();

        createRoom.setDelegate(createRoomResponse);
        CreateRoom.UserCreateRoom userCreateRoom = new CreateRoom.UserCreateRoom();
        userCreateRoom.setUser_ID(new Long(MyApplication.getOwnUser().getUser_ID()));
        userCreateRoom.setRoom_CreateTime(TimeUtil.currentTime());
        userCreateRoom.setRoom_Desc(str_desc);
        userCreateRoom.setRoom_MaxNum(Integer.parseInt(str_maxNum));
        userCreateRoom.setRoom_Name(str_name);
        userCreateRoom.setRoom_Topic(str_topic);

        createRoom.setUserCreateRoom(userCreateRoom);
        createRoom.create();

    }
}
